package com.slz3.business.module.goodsManagement.gooBaseGoodsSpecification.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.stream.Collectors;

public class GooBaseGoodsSpecificationFactory {

    /**
     * 初始化基础属性值
     */
    public static GooBaseGoodsSpecification initBaseFields(GooBaseGoodsSpecification gooBaseGoodsSpecification) {
        if (gooBaseGoodsSpecification.getUuid() == null) {
            gooBaseGoodsSpecification.setUuid(UUIDUtils.generateUUID());
            gooBaseGoodsSpecification.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(gooBaseGoodsSpecification.getSerAudit())) {
                gooBaseGoodsSpecification.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(gooBaseGoodsSpecification.getSerUsing())) {
                gooBaseGoodsSpecification.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            gooBaseGoodsSpecification.setLastModifyTime(LocalDateTime.now());
        }
        return gooBaseGoodsSpecification;
    }

    /**
     * 初始化基础属性值
     */
    public static GooBaseGoodsSpecification initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static GooBaseGoodsSpecification newTemStyle() {
        return new GooBaseGoodsSpecification();
    }

    public static GooBaseGoodsSpecificationCondition newGooBaseGoodsSpecificationCondition(MultiValueMap<String, String> params) {

        GooBaseGoodsSpecificationCondition condition = new GooBaseGoodsSpecificationCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, GooBaseGoodsSpecificationCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, GooBaseGoodsSpecificationCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, GooBaseGoodsSpecificationCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, GooBaseGoodsSpecificationCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

}
